package com.yuanfengerp.purchase.service.impl;


import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.purchase.dao.IPurchaseOrderDao;
import com.yuanfengerp.purchase.entity.PurchaseOrder;
import com.yuanfengerp.purchase.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService {
    @Autowired
    private IPurchaseOrderDao ipod;

    @Override
    public Result<List<PurchaseOrder>> queryPurchaseOrder(Map<String, Object> params) {
        List<PurchaseOrder> purchaseOrders = ipod.queryPurchaseOrder(params);
        if (!purchaseOrders.isEmpty()) {
            return Result.success(purchaseOrders);
        } else {
            return Result.failure(400, "暂无数据");
        }
    }

    @Override
    public Result<String> addPurchaseOrder(PurchaseOrder purchaseOrder) {
        purchaseOrder.setPurchase_order_expected_arrival_date(new Date());
        purchaseOrder.setPurchase_order_operation_time(new Date());
        purchaseOrder.setPurchase_order_audit_time(new Date());
        int i = ipod.addPurchaseOrder(purchaseOrder);
        if (i > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failure(400, "添加失败");
        }
    }

    @Override
    public Result<PurchaseOrder> queryPurchaseOrderById(String purchase_order_id) {
        PurchaseOrder purchaseOrder = ipod.queryPurchaseOrderById(purchase_order_id);
        if (purchaseOrder != null) {
            return Result.success(purchaseOrder);
        } else {
            return Result.failure(400, "暂无数据");
        }
    }

    @Override
    public Result<String> updatePurchaseOrderById(PurchaseOrder purchaseOrder) {
        int i = ipod.updatePurchaseOrderById(purchaseOrder);
        if (i > 0) {
            return Result.success("修改成功");
        } else {
            return Result.failure(400, "修改失败");
        }
    }

    @Override
    public Result<String> deletePurchaseOrder(String purchase_order_id) {
        int i = ipod.deletePurchaseOrder(purchase_order_id);
        if (i > 0) {
            return Result.success("删除成功");
        } else {
            return Result.failure(400, "删除失败");
        }
    }

    @Override
    public Result<String> updatePurchaseOrderAgree(String purchase_order_id) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝，如果不是则将订单状态改为1
        System.out.println(purchase_order_id);
        int i = ipod.updatePurchaseOrderAgree(purchase_order_id);
        if (i > 0) {
            return Result.success(purchase_order_id + "审核通过");
        } else {
            return Result.failure(400, "审核失败");
        }
    }

    @Override
    public Result<String> updatePurchaseOrderRefuse(String purchase_order_id) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝，如果不是则将订单状态改为1
        System.out.println(purchase_order_id);
        int i = ipod.updatePurchaseOrderAgree(purchase_order_id);
        if (i > 0) {
            return Result.success(purchase_order_id + "审核拒绝");
        } else {
            return Result.failure(400, "审核失败");
        }
    }
}
